//
//  YDSelectPopupView.m
//  YunDongGym
//
//  Created by lqq on 2018/4/19.
//  Copyright © 2018年 lqq. All rights reserved.
//

#import "YDSelectPopupView.h"


@interface YDSelectPopupView () <UITableViewDelegate,UITableViewDataSource,UIGestureRecognizerDelegate>
@property (nonatomic, strong) NSArray *titles;
@property (nonatomic, copy) void(^selectBlock)(NSInteger selectIndex);
@property (nonatomic, copy) void(^cancleBlock)();
@property (nonatomic, strong) UITableView *tableView;
@end
@implementation YDSelectPopupView

+ (void)showWithTitles:(NSArray *)titles selectBlock:(void(^)(NSInteger selectIndex))selectBlock cancleBlock:(void(^)())canclEBlock{
    YDSelectPopupView *popupView = [[YDSelectPopupView alloc] init];
    popupView.titles = titles;
    popupView.selectBlock = selectBlock;
    popupView.cancleBlock = canclEBlock;
    [popupView show];
}


- (void)show{
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    tableView.dataSource = self;
    tableView.delegate = self;
    tableView.backgroundColor = [UIColor whiteColor];
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [tableView registerClass:[YDSelectPopupCell class] forCellReuseIdentifier:NSStringFromClass([YDSelectPopupCell class])];
    
    [self addSubview:tableView];
    self.tableView = tableView;
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(48);
        make.right.equalTo(self).offset(-48);
        if (self.cancleBlock) {
            make.centerY.equalTo(self).offset(-40);
        } else {
            make.centerY.equalTo(self);

        }
        make.height.equalTo(@(55*self.titles.count));
    }];
    
    if (_cancleBlock) {
        UIButton *cancleBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        cancleBtn.layer.cornerRadius = 25;
        cancleBtn.layer.masksToBounds = YES;
        [cancleBtn setBackgroundColor:[UIColor colorWithWhite:0 alpha:0]];
        [cancleBtn setImage:IMG_NAME(@"close_icon") forState:UIControlStateNormal];
        [cancleBtn addTarget:self action:@selector(cancleBtnClicked) forControlEvents:UIControlEventTouchUpInside];
        [cancleBtn setTintColor:[UIColor whiteColor]];
        cancleBtn.titleLabel.font = MediumFont(40);
        [self addSubview:cancleBtn];
        [cancleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.height.equalTo(@(50));
            make.centerX.equalTo(self);
            make.top.equalTo(tableView.mas_bottom).offset(30);
        }];
    } else {
        UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] init];
        [tapGes addTarget:self action:@selector(handleGesture:)];
        tapGes.delegate = self;
        [self addGestureRecognizer:tapGes];
    }
    
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    self.frame = window.bounds;
    [window addSubview:self];
    
    self.backgroundColor = [UIColor clearColor];
    [UIView animateWithDuration:0.3 animations:^{
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
    }];
    

    
}

- (void)cancleBtnClicked
{
    if (_cancleBlock) {
        _cancleBlock();
    }
    [self removeFromSuperview];
}

- (void)handleGesture:(UITapGestureRecognizer *)tap
{
    CGPoint point = [tap locationInView:self];
    if (!CGRectContainsPoint(self.tableView.frame, point)) {
        [self removeFromSuperview];
    }
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.titles.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YDSelectPopupCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([YDSelectPopupCell class])];
    cell.centerLabel.text = self.titles[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.selectBlock) {
        self.selectBlock(indexPath.row);
    }
    [self removeFromSuperview];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 55;
}

#pragma mark - UIGestureRecognizerDelegate
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) {//判断如果点击的是tableView的cell，就把手势给关闭了
        return NO;//关闭手势
    }//否则手势存在
    return YES;
}


@end


@implementation YDSelectPopupCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.centerLabel = [[UILabel alloc] init];
        self.centerLabel.textColor = HEXColor(@"333333");
        self.centerLabel.font = RegularFont(14);
        [self.contentView addSubview:self.centerLabel];
        [self.centerLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.centerY.equalTo(self.contentView);
        }];
        
        UIView *lineView = [[UIView alloc] init];
        lineView.backgroundColor = HEXColor(@"E5E5E5");
        [self.contentView addSubview:lineView];
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView).offset(25);
            make.right.equalTo(self.contentView).offset(-25);
            make.bottom.equalTo(self.contentView);
            make.height.equalTo(@(kOnePX));
        }];
    }
    return self;
}
@end








